package com.base.controller;

import java.io.PrintWriter;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import com.base.comm.CommContent;
import com.base.po.voip.CompanyNews;
import com.base.service.CompanyNewsService;
import com.base.service.LogsService;
import com.base.utils.IP;

/**
 * admin管理系统控制器
 * 
 * @author Administrator
 *
 */
@Controller
@RequestMapping("/admin")
public class content_chakandongtai {
	@Autowired
	private CompanyNewsService companyNewsService;
	@Autowired
	private LogsService logsService;// log日志
	
	// 用户首页
	@RequestMapping("/content_chakandongtai")
	public ModelAndView index(HttpServletRequest request,String id) throws Exception {
		// 返回ModelAndView
		ModelAndView modelAndView = new ModelAndView();
		if (request.getSession().getAttribute("adminUserId") == null) {
			modelAndView.setViewName("jsp/login");
			return modelAndView;
		}
		
		if (id == null) {
			modelAndView.setViewName("jsp/datanotfound");
			return modelAndView;
		}
		
		CompanyNews companyNews = companyNewsService.selectByPrimaryKey(id);
		if(companyNews != null) {
			request.setAttribute("id", id);
			request.setAttribute("title", companyNews.getTitle());
			request.setAttribute("contenttitle", companyNews.getContenttitle());
			request.setAttribute("parentid", companyNews.getParentid());
			if(companyNews.getType() == 1) {
				request.setAttribute("receiveid", companyNews.getReceiveid());
			}
			request.setAttribute("content", companyNews.getContent());
			request.setAttribute("type", companyNews.getType());
			request.setAttribute("username", companyNews.getUsername());
			request.setAttribute("url", companyNews.getUrl());
			request.setAttribute("pic", companyNews.getPic());
			request.setAttribute("contentpic", companyNews.getContentpic());
			request.setAttribute("theme", companyNews.getTheme());
			// 设置页面进行跳转
			modelAndView.setViewName("jsp/content_chakandongtai");
			return modelAndView;
		}
		
		modelAndView.setViewName("jsp/datanotfound");
		return modelAndView;
	}
	
	String SUCCESS = "success";
	@RequestMapping(value = "/updateCompanyNews", method = RequestMethod.POST)
	public void updateCompanyNews(HttpServletRequest request, HttpServletResponse response,
			@RequestParam(value = "id", required = true) String id, Byte datastatus, String url,
			String title, String contenttitle, String username, String receiveid, String theme,
			String content, String contentPic, String pic) throws Exception {
		// 返回参数
		response.setCharacterEncoding("utf-8");
		response.setContentType("text/html;charset=UTF-8");
		String parent = (String) request.getSession().getAttribute("adminUserId");
		
		CompanyNews companyNews = companyNewsService.selectByPrimaryKey(id);

		PrintWriter out = response.getWriter();
	
			if (id == null) {
				out.print("浏览器在点击的过程中发现丢失了用户信息，请您刷新重试！");
				return;
			} else {
				companyNews.setDatastatus(datastatus);
				companyNews.setUrl(url);
				companyNews.setTitle(title);
				companyNews.setContenttitle(contenttitle);
				companyNews.setUsername(username);
				companyNews.setReceiveid(receiveid);
				companyNews.setTheme(theme);
				companyNews.setContent(content);
				companyNews.setContentpic(contentPic);
				companyNews.setPic(pic);
				int code = companyNewsService.updateByPrimaryKeySelective(companyNews);
				
				String usernamesys = (String) request.getSession().getAttribute("useracc");
                 
				if (code == 1) {
					logsService.insert(CommContent.TYPE_DELET_HUAZHANG, "用户：" + usernamesys + "修改1条企业动态", usernamesys,
							true, IP.getIpAddr(request));
					out.print(SUCCESS);
				} else {
					logsService.insert(CommContent.TYPE_DELET_HUAZHANG, "用户：" + usernamesys + "修改企业动态失败", usernamesys,
							true, IP.getIpAddr(request));
					out.print("更改失败，请您重试");
				}
		}
	}
}
